package com.jf.cloud.product.listener;

import com.jf.cloud.api.multishop.feign.ShopSubstituteSalesFeignClient;
import com.jf.cloud.common.product.constant.IsSynUpdate;
import com.jf.cloud.common.product.constant.SalesType;
import com.jf.cloud.common.product.vo.SpuVO;
import com.jf.cloud.common.rocketmq.config.RocketMqConstant;
import com.jf.cloud.common.vo.ShopSubstituteSalesVO;
import com.jf.cloud.product.service.SpuService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zz
 * @date 2022/11/3 9:12
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.SUPPLIER_PRODUCT_UPDATE,consumerGroup = RocketMqConstant.SUPPLIER_PRODUCT_UPDATE)
public class SupplierSpuUpdateNotifyConsumer implements RocketMQListener<Long> {

    @Autowired
    private SpuService spuService;

    @Autowired
    private ShopSubstituteSalesFeignClient shopSubstituteSalesFeignClient;

    /**
     * 供应商品基本信息更新后给供应商返发货的且是手动设价的代销商品添加提示
     * @param supplierId
     */
    @Override
    public void onMessage(Long supplierId) {
        List<SpuVO> commissionSpuList = spuService.getCommissionSpuList(supplierId);
        List<Long> shopIdList = commissionSpuList.stream().map(SpuVO::getShopId).collect(Collectors.toList());
        if (Objects.isNull(shopIdList) || shopIdList.size() == 0){
            return;
        }
        List<ShopSubstituteSalesVO> shopSubstituteSalesVOList = shopSubstituteSalesFeignClient.shopSubstituteSalesList(shopIdList).getData();
        //满足条件的商家id
        List<Long> shopIdS = new ArrayList<>();
        for (ShopSubstituteSalesVO salesVO : shopSubstituteSalesVOList) {
            if (Objects.equals(salesVO.getSalesType(), SalesType.MANUAL_OPERATION_PRICE.value())){
                shopIdS.add(salesVO.getShopId());
            }
        }
        //满足条件的代销商品id
        List<Long> spuIdList = new ArrayList<>();
        for (SpuVO spuVO : commissionSpuList) {
            if (shopIdS.contains(spuVO.getShopId())){
                spuIdList.add(spuVO.getSpuId());
            }
        }
        spuService.batchChangeSynUpdate(spuIdList, IsSynUpdate.NO_CHANGE.value());
    }
}
